﻿// -----------------------------------------------------------------------
// <copyright file="CustomMenu.cs" company="Abyssalsoft">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace Gui.ControlBuilders
{
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Xml;
    using Metal.Config;
    using Metal.Resources;
    using Gui.ThemeEngine;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class CustomMenu : BaseControl
    {
        /// <summary>
        /// Create the custom control
        /// </summary>
        /// <param name="controlProperties">The Xml node that contains the control properties</param>
        /// <returns>Returns custom control</returns>
        public Controls.MetalTopMenu GetControl(XmlNode controlProperties)
        {
            string strThemeName = Settings.Instance.GetValue("LastSettings.Theme", "BlackMetal");

            Controls.MetalTopMenu customMenu = new Controls.MetalTopMenu();

            // Apply common properties
            this.BuildControl(controlProperties, customMenu as Control);

            // Load ContextMenu style
            string strConfigFile = Paths.ThemesFolder + strThemeName + "\\contextmenu.xml";
            XmlDocument xmlConfig = new XmlDocument();
            xmlConfig.Load(strConfigFile);
            XmlNode menuItem = xmlConfig.SelectSingleNode("ContextMenu/MenuItem");
            XmlNode dropDownItem = xmlConfig.SelectSingleNode("ContextMenu/DropDown");

            // Set font
            FontStyle styleMenuItem = (FontStyle)Enum.Parse(typeof(FontStyle), PropertyProvider.GetValue(controlProperties, "fontStyle", "Regular"));
            customMenu.SetFont(PropertyProvider.GetValue(controlProperties, "font", "Default"), PropertyProvider.GetValue(controlProperties, "fontSize", 10), styleMenuItem);

            // Apply specific control properties
            customMenu.MenuBackground = Image.FromFile(Paths.ThemesFolder + strThemeName + "\\" + PropertyProvider.GetValue(controlProperties, "image", "no-image.png"));

            // Menu (Menu1)
            foreach (XmlNode menu in controlProperties.ChildNodes)
            {
                ToolStripMenuItem item = new ToolStripMenuItem();
                item.Name = PropertyProvider.GetValue(menu, "command", string.Empty);
                string strText = MessageProvider.GetMessage(PropertyProvider.GetValue(menu, "resourceID", string.Empty));
                item.Text = strText;
                item.ForeColor = PropertyProvider.GetValue(menu, "foreColor");
                ////item.Image = Image.FromFile(Metal.Config.Paths.ThemesFolder + strThemeName + "\\" + PropertyProvider.GetValue(menu, "icon", "no-image.png"));

                // Menu item
                Controls.MetalTopMenu.menuItemSelected = PropertyProvider.GetValue(menuItem, "colorSelected");
                Controls.MetalTopMenu.menuForeColor = PropertyProvider.GetValue(menuItem, "foreColor");
                Controls.MetalTopMenu.menuItemSelectedGradientBegin = PropertyProvider.GetValue(menuItem, "hoverColorGradientBegin");
                Controls.MetalTopMenu.menuItemSelectedGradientEnd = PropertyProvider.GetValue(menuItem, "hoverColorGradientEnd");
                Controls.MetalTopMenu.menuItemPressedGradientBegin = PropertyProvider.GetValue(menuItem, "pressedGradientBegin");
                Controls.MetalTopMenu.menuItemPressedGradientEnd = PropertyProvider.GetValue(menuItem, "pressedGradientEnd");
                Controls.MetalTopMenu.menuBorder = PropertyProvider.GetValue(menuItem, "colorBorder");
                Controls.MetalTopMenu.menuItemBorder = PropertyProvider.GetValue(menuItem, "colorBorder");

                // Drop down
                Controls.MetalTopMenu.dropDownMenuBackColor = PropertyProvider.GetValue(dropDownItem, "backColor");
                Controls.MetalTopMenu.dropDownMenuForeColor = PropertyProvider.GetValue(dropDownItem, "foreColor");
                Controls.MetalTopMenu.imageMarginGradientBegin = PropertyProvider.GetValue(dropDownItem, "imageMarginGradientBegin");
                Controls.MetalTopMenu.imageMarginGradientMiddle = PropertyProvider.GetValue(dropDownItem, "imageMarginGradientMiddle");
                Controls.MetalTopMenu.imageMarginGradientEnd = PropertyProvider.GetValue(dropDownItem, "imageMarginGradientEnd");

                // SubmenuItems (SubItem1)
                foreach (XmlNode subItem in menu.ChildNodes)
                {
                    ToolStripMenuItem subItemMenu = new ToolStripMenuItem();

                    if (PropertyProvider.GetValue(subItem, "command", string.Empty) == "Separator")
                    {
                        ////subItemMenu.DropDownItems.Add(new ToolStripSeparator());
                        ////topMenu.AddSeparator();
                    }
                    else
                    {
                        subItemMenu.Name = PropertyProvider.GetValue(subItem, "command", "menu");
                        strText = MessageProvider.GetMessage(PropertyProvider.GetValue(subItem, "resourceID", string.Empty));
                        subItemMenu.Text = strText;
                        ////subItemMenu.Image = Image.FromFile(Metal.Config.Paths.ThemesFolder + strThemeName + "\\" + PropertyProvider.GetValue(subItem, "icon", "no-image.png"));
                        item.DropDownItems.Add(subItemMenu);
                    }

                    // SubmenuItemsChild (SubItemsChild1)
                    foreach (XmlNode subItemChild in subItem.ChildNodes)
                    {
                        ToolStripMenuItem subItemMenuChild = new ToolStripMenuItem();
                        if (PropertyProvider.GetValue(subItemChild, "command", string.Empty) == "Separator")
                        {
                            ////subItemMenuChild.DropDownItems.Add(new ToolStripSeparator());
                            ////topMenu.AddSeparator();
                        }
                        else
                        {
                            subItemMenuChild.Name = PropertyProvider.GetValue(subItemChild, "command", "menu");
                            strText = MessageProvider.GetMessage(PropertyProvider.GetValue(subItemChild, "resourceID", string.Empty));
                            ////subItemMenuChild.Image = Image.FromFile(Metal.Config.Paths.ThemesFolder + strThemeName + "\\" + PropertyProvider.GetValue(subItemChild, "icon", "no-image.png"));
                            subItemMenuChild.Text = strText;

                            subItemMenu.DropDownItems.Add(subItemMenuChild);
                        }
                    }
                }

                customMenu.AddMenu(item);
            }

            return customMenu;
        }

        /// <summary>
        /// Apply common properties
        /// </summary>
        /// <param name="control">The Xml node that contains our control properties</param>
        /// <param name="customControl">The custom control that will be created</param>
        public override void BuildControl(System.Xml.XmlNode control, System.Windows.Forms.Control customControl)
        {
            base.BuildControl(control, customControl);
        }
    }
}
